﻿// <copyright file="MainMenuViewModel.cs" company="EffuEfface">
//      Copyright (c) Effu Efface Consulting  All rights reserved.
// </copyright>
// <author>Ryan R. Sites</author>

namespace TVersiPlay.MainMenu.ViewModels
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Input;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Unity;
    using TVersiPlay.Common;
    using TVersiPlay.Common.Enumerations;
    using TVersiPlay.Common.Interfaces;
    using TVersiPlay.Events;

    class MainMenuViewModel: IMainMenuViewModel
    {
        #region Delcarations

        private IUnityContainer container;
        private INavigationService navigate;
        private IUPnPControlPointService upnpService;
        private IEventAggregator eventAggregator;

        private ObservableCollection<MenuItem> myMenuItems;
        private int selectedMenuIndex = 0;
        private string selectedMenuText;
        private string selectedMenuDescription;
        private string selectedMenuTooltip;

        private ICommand selectedMenuPressed;

        #endregion

        #region Constructor

        public MainMenuViewModel(IUnityContainer container, INavigationService navigate, IUPnPControlPointService upnpService, IEventAggregator eventAggregator)
        {
            this.myMenuItems = new ObservableCollection<MenuItem>();
            this.LoadMenuItems();
            this.selectedMenuPressed = new DelegateCommand<object>(this.OnSelectedMenuPressed);
            this.container = container;
            this.eventAggregator = eventAggregator;

            this.navigate = navigate;
            this.upnpService = upnpService;
            this.SetMenuProperties(0);           
        }

        #endregion

        #region Properties

        public ObservableCollection<MenuItem> MyItems
        {
            get
            {
                return this.myMenuItems;
            }

        }

        public int SelectedMenuIndex
        {
            get
            {
                return this.selectedMenuIndex;
            }

            set
            {
                this.selectedMenuIndex = value;
                this.SetMenuProperties(value);
                this.OnPropertyChanged("SelectedMenuIndex");

            }
        }

        public string SelectedMenuDescription
        {
            get
            {
                return this.selectedMenuDescription;

            }
            set
            {
                if (value != this.selectedMenuDescription)
                {
                    this.selectedMenuDescription = value;
                    OnPropertyChanged("SelectedMenuDescription");
                }
            }
        }

        public ICommand SelectedMenuPressed
        {
            get { return this.selectedMenuPressed; }
        }

        #endregion


        #region Methods

        private void OnSelectedMenuPressed(object parameter)
        {
            //this.eventAggregator.GetEvent<LongRunningProcessEvent>().Publish(new DTO.LongRunningProcess(){IsBusy = true, BusyMessage = "Navigating...", ShowPopUp = false});

            switch (this.selectedMenuText)
            {                
                case "Application Settings":
                    this.navigate.NavigateViewToRegion(ViewNames.AppSettingsView, RegionNames.MainRegion);
                    break;
                case "Music":
                    if (this.upnpService.TVersityServerAvailable())
                    {                        
                        this.navigate.NavigateViewToRegion(ViewNames.MediaBrowseView, RegionNames.MainRegion);
                        this.upnpService.BrowseMediaServer(BrowseType.Audio, ContentDirectory.Enum_A_ARG_TYPE_BrowseFlag.BROWSEDIRECTCHILDREN, this.BrowseResultCallback);
                    }
                    break;
                case "Pictures":
                    if (this.upnpService.TVersityServerAvailable())
                    {
                        this.navigate.NavigateViewToRegion(ViewNames.MediaBrowseView, RegionNames.MainRegion);
                        this.upnpService.BrowseMediaServer(BrowseType.Photo, ContentDirectory.Enum_A_ARG_TYPE_BrowseFlag.BROWSEDIRECTCHILDREN, this.BrowseResultCallback);
                    }
                    break;
                case "Movies":
                    if (this.upnpService.TVersityServerAvailable())
                    {
                        this.navigate.NavigateViewToRegion(ViewNames.MediaBrowseView, RegionNames.MainRegion);
                        this.upnpService.BrowseMediaServer(BrowseType.Video, ContentDirectory.Enum_A_ARG_TYPE_BrowseFlag.BROWSEDIRECTCHILDREN, this.BrowseResultCallback);
                    }
                    break;
                case "Internet Content":
                    if (this.upnpService.TVersityServerAvailable())
                    {
                        this.navigate.NavigateViewToRegion(ViewNames.MediaBrowseView, RegionNames.MainRegion);
                        this.upnpService.BrowseMediaServer(BrowseType.Internet, ContentDirectory.Enum_A_ARG_TYPE_BrowseFlag.BROWSEDIRECTCHILDREN, this.BrowseResultCallback);
                    }
                    break;
                default:
                    if (!this.upnpService.TVersityServerAvailable())
                        this.navigate.NavigateViewToRegion(ViewNames.AppSettingsView, RegionNames.MainRegion);
                    break;
                    
            }
        }

        private void BrowseResultCallback(DTO.BrowseResults browseResults)
        {

            this.eventAggregator.GetEvent<LongRunningProcessEvent>().Publish(new DTO.LongRunningProcess() {IsBusy = false});

            this.eventAggregator.GetEvent<BrowseResultEvent>().Publish(browseResults);           
        }

        private void LoadMenuItems()
        {
            using (System.Drawing.Bitmap bitmap = Properties.Resources.sound)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();

                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                myMenuItems.Add(new MenuItem() { MenuDescription = "Play music files located by TVersity Media Server", MenuImage = bitmapSource, MenuTitle = "Music", ToolTip = "Music" });
            }

            using (System.Drawing.Bitmap bitmap = Properties.Resources.movie_track)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();

                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                myMenuItems.Add(new MenuItem() { MenuDescription = "Watch video files located by TVersity Media Server", MenuImage = bitmapSource, MenuTitle = "Movies", ToolTip = "Movies" });
            }

            using (System.Drawing.Bitmap bitmap = Properties.Resources.image)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();

                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                myMenuItems.Add(new MenuItem() { MenuDescription = "Browse image files located by TVersity Media Server", MenuImage = bitmapSource, MenuTitle = "Pictures", ToolTip = "Pictures" });
            }

            using (System.Drawing.Bitmap bitmap = Properties.Resources.rss)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();

                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                myMenuItems.Add(new MenuItem() { MenuDescription = "Preview internet content located by TVersity Media Server", MenuImage = bitmapSource, MenuTitle = "Internet Content", ToolTip = "Internet Content" });
            }

            using (System.Drawing.Bitmap bitmap = Properties.Resources.chart)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();

                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                myMenuItems.Add(new MenuItem() { MenuDescription = "View Reports provided by TVersiplay", MenuImage = bitmapSource, MenuTitle = "Reports", ToolTip = "Reports" });
            }

            using (System.Drawing.Bitmap bitmap = Properties.Resources.process)
            {
                IntPtr hBitmap = bitmap.GetHbitmap();

                System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                myMenuItems.Add(new MenuItem() { MenuDescription = "Access settings and tools for TVersiPlay", MenuImage = bitmapSource, MenuTitle = "Application Settings", ToolTip = "Application Settings" });
            }
        }

        private void SetMenuProperties(int selectedIndex)
        {
            if (this.myMenuItems != null && this.myMenuItems.Count > 0)
            {
                this.SelectedMenuDescription = this.myMenuItems[selectedIndex].MenuDescription;
                this.selectedMenuText = this.myMenuItems[selectedIndex].MenuTitle;
                this.selectedMenuTooltip = this.myMenuItems[selectedIndex].ToolTip;
            }
        }

        #endregion



        #region INotifyPropertyChanged Items

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null)
                Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}
